<?php

class Dashboard extends CI_Controller {

    function __constuct() {
        parent::__construct();
        $this->load->helper('url');
    }

    function index() {
        $this->load->view('admin/main_layout');
    }

    function forum() {
        $this->load->view('admin/forum_layout');
    }

    function user() {
        $this->load->model('users_mgr');
        $this->load->library('pagination');
        $this->session->set_userdata('count_member', $this->db->count_all('user'));

        $config['base_url'] = site_url('admin/dashboard/user');
        $config['total_rows'] = $this->db->count_all('user');
        $config['per_page'] = 10;
        $config["uri_segment"] = 4;

        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
        $this->pagination->initialize($config);

        $data['pages'] = $this->pagination->create_links();
        $data['title'] = 'All Users';
        $data['member'] = $this->users_mgr->all_users($config['per_page'], $page);

        $this->load->view('admin/user_layout', $data);
    }

    //delete here
    function delete_user() {
        $this->load->model('users_mgr');
        $id = $this->uri->segment(4);
        if ((int) $id > 0) {
            $oldrow = array('id' => $id);
            $this->users_mgr->delete_users($oldrow);

            redirect('admin/dashboard/user');
        }
    }

    //edit function by Miss. Sambo
    function select_edit() {
        $this->load->model('users_mgr');
        $data['user_edit'] = $this->users_mgr->select_edit_user();
        $data['title'] = 'Update user';

        $this->load->view('admin/select_edit', $data);
    }

    // *********** Update user *********
    function update_user() {
		if ($this->input->post('btn_submit')) {
			$this->load->model('users_mgr');
			$data['member'] = $this->users_mgr->update_user();
			if ($data['member'] > 0) {
				redirect('admin/dashboard/user');
			}
		}
    }

    // ======== End Update  user =======
    //add user
    function add_user() {
        $data['title'] = 'Add Members';
		if ($this->input->post('btn_submit')) {
			$this->load->model('users_mgr');
			$this->load->library("form_validation");

			$this->form_validation->set_rules('gender', 'Gender', 'trim|required');
			$this->form_validation->set_rules('nationality', 'nationality', 'trim|required');
			$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_alpha_numeric|min_length[6]|xss_clean|strtolower|callback_username_not_exists');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[6]|valid_email');
			//$this->form_validation->set_rules('role','Role user', 'trim|required');
			$this->form_validation->set_rules('pass', 'Password', 'trim|required|alpha_alpha_numeric|min_length[6]|matches[conf_pass]|xss_clean');
			$this->form_validation->set_rules('conf_pass', 'Confirm_password', 'trim|required|alpha_alpha_numeric|min_length[6]');

			if ($this->form_validation->run() == FALSE) {
				//hasn't been run or there are validation errors
				$this->load->view('admin/add_user', $data);
			} else {
				$result['reg'] = $this->users_mgr->addmembers();

				if ($result) {
					redirect('admin/dashboard/user');
				} else {
					echo 'error';
				}
			}
		} else {
			$this->load->view('admin/add_user', $data);
		}
    }

    // end fun add members ==========
    // fun username not exists in record.....
    function username_not_exists($username) {
        $this->load->model('users_mgr');
        $this->form_validation->set_message('username_not_exists', 'That %s already exists. Please choose a different Username !');
        //redirect('con_login/login');
        if ($this->users_mgr->checkexist($username)) {
            return false;
        } else {
            return true;
        }
    }

}
